const elementResizeDetectorMaker = require("element-resize-detector")
// 混入js 
export default {
    methods: {
        notify(type, msg) {
            const base_obj = {
                position: 'top-left',
                showClose: false,
                message: msg,
                duration: 1000
            }
            switch (type) {
                case 'success':
                    this.$notify.success({
                        ...base_obj,
                        title: '成功'
                    })
                    break
                case 'error':
                    this.$notify.error({
                        ...base_obj,
                        title: '错误'
                    })
                    break
                case 'info':
                    this.$notify.info({
                        ...base_obj,
                        title: '提示'
                    })
                    break
                case 'warn':
                    this.$notify.warning({
                        ...base_obj,
                        title: '警告'
                    })
                    break
                default:
                    console.error('不支持的通知类型', type)
            }
        },
        no() {

        },
        createErd() {
            const erd = elementResizeDetectorMaker()
            const erdUltraFast = elementResizeDetectorMaker({
                strategy: "scroll",
                callOnAdd: true
            });
            return erd
        }
    },
    data(){
        return {
            file_icon_map:{
                'code_text':'../code_text.png'
            }
        }
    }
}
// 用户校验规则
export const user_validator = {
    data() {
        const username_validator = (rule, value, callback) => {
            if (!/[a-z0-9_@\$]{4,}/.test(value)) {
                callback(new Error('用户名长度过短'))
            } else {
                callback()
            }
        }
        const password_validator = (rule, value, callback) => {
            if (!/[\S]{6,}/.test(value)) {
                callback(new Error('密码长度过短'))
            } else {
                callback()
            }
        }
        const password_twice_validator = (rule, value, callback) => {
            if (this.user.password != value) {
                callback(new Error('两次输入密码不一致'))
            } else {
                callback()
            }
        }
        const rules = {
            username: [{
                validator: username_validator,
                trigger: 'blur'
            }],
            password: [
                {
                    validator: password_validator,
                    trigger: 'blur'
                }
            ],
            password_twice: [
                {
                    validator: password_twice_validator,
                    trigger: 'blur'
                }
            ]
        }
        return {
            rules
        }
    }
}
export const items = {
    data() {
        return {
            items: {
                'text-editor': [{
                    id: 1,
                    options: [
                        { value: 1, label: "新建文本文件" },
                        { value: 2, label: "新建文件" },
                        { value: 3, label: "打开文件" }
                    ],
                    selected: "文件",
                    changeMode(v) {
                        this.ItemOption.options.forEach((e) => {
                            if (e.value == v) {
                                this.$pubsub.publish(this.process.process_id, e.label);
                                this.ItemOption.selected = e.label;
                            }
                        });
                    },
                    offset: 0,
                    span: 2
                },
                {
                    id: 2,
                    options: [
                        { value: 1, label: "JavaScript" },
                        { value: 2, label: "Java" },
                        { value: 3, label: "C++" },
                        { value: 4, label: "Python" },
                        { value: 5, label: "C" },
                    ],
                    selected: "JAVA",
                    changeMode(v) {
                        this.ItemOption.options.forEach((e) => {
                            if (e.value == v) {
                                this.$pubsub.publish(this.process.process_id, e.label);
                                this.ItemOption.selected = e.label;
                            }
                        });
                    },
                    offset: 2,
                    span: 2
                },
                {
                    id: 3,
                    options: [
                        { value: 1, label: "打开控制台" },
                        {value:2,label:"关闭控制台"}
                    ],
                    selected: "控制台",
                    changeMode(v) {
                        switch(v){
                            case 1:
                                this.$pubsub.publish(`showtab:${this.process.process_id}`)
                                break
                            case 2:
                                this.$pubsub.publish(`closetab:${this.process.process_id}`)
                        }
                    },
                    offset: 2,
                    span: 2
                },
                {
                    id: 4,
                    options: [
                        { value: 1, label: "运行代码" }
                    ],
                    selected: "运行",
                    changeMode(v) {
                        switch(v){
                            case 1:
                                // 获取fileId
                              const fileId = this.process.meta.file.fileId
                                this.$ms.run_code(fileId,this.token).then((resp)=>{
                                if(resp.data.code!=200){
                                    this.notify('error',resp.data.msg)
                                }else{
                                    // 获得运行结果
                                   // console.log(resp.data.data)
                                   this.$set(this.process.meta,'run_result',resp.data.data)
                                   this.$pubsub.publish(`showtab:${this.process.process_id}`)
                                }
                                },(resp)=>{
                                    this.notify('error',`[ ${resp.name} ] ${resp.message}`)
                                })
                        }
                    },
                    offset: 2,
                    span: 2
                }
                ],
                'folder-manager': [{
                    id: 1,
                    options: [
                        { value: 1, label: "新建文件夹" },
                        { value: 2, label: "新建文件" }
                    ],
                    selected: "新建",
                    changeMode(v) {
                        if(this.meta.node == undefined){
                            this.notify('warn','[ 提醒 ] 你还未指定文件夹')
                            return
                        }
                        if(this.meta.node.data.fileType=='文件'){
                            this.notify('warn','[ 提醒 ] 请指定文件夹')
                            return
                        }
                        const node = this.meta.node
                        // 获取当前node.data.fileId
                        const fileId = node.data.fileId
                        switch(v){
                            case 1:
                                // 新建文件夹
                                // 获取当前node
                                //console.log(this)
                                this.$ms.create_dir(fileId,this.token).then((resp)=>{
                                    if(resp.data.code!=200){
                                        this.notify('error',resp.data.msg)
                                    }else{
                                        const newDir = {
                                            ...resp.data.data,
                                            isleaf:true,
                                            isShow:false,
                                            fileType:'文件夹'
                                        }
                                        this.$pubsub.publish(`appendNode:${this.process.process_id}`,{data:newDir,node})
                                        // 桌面更新
                                        this.$pubsub.publish('insert_desk_file',resp.data.data)
                                    }
                                },(resp)=>{
                                    this.notify('error',`[ ${resp.name} ] ${resp.message}`)
                                })

                            break
                            case 2:
                                this.$ms.create_file(fileId,this.token).then((resp)=>{
                                    if(resp.data.code!=200){
                                        this.notify('error',resp.data.msg)
                                    }else{
                                        const newFile = {
                                            ...resp.data.data,
                                            isleaf:true,
                                            isShow:false,
                                            fileType:'文件'
                                        }
                                        this.$pubsub.publish(`appendNode:${this.process.process_id}`,{data:newFile,node})
                                        this.$pubsub.publish('insert_desk_file',resp.data.data)
                                    }
                                },(resp)=>{
                                    this.notify('error',`[ ${resp.name} ] ${resp.message}`)
                                })
                            break
                            default:
                        }
                    },
                    offset: 0,
                    span: 2
                }
                ]
            },
            meta:new Object()
        }
    },
    mounted() {
        // 注册回调函数 获取显示区域的meta数据
        this.$pubsub.subscribe(`getMeta:${this.process.process_id}`,(name,meta)=>{this.meta[meta.name]=meta.data})
    }
}